/**

* <p>Title: ShopcController.java</p>

* <p>Description: </p>

* <p>Copyright: Copyright (c) 2021</p>

* <p>Company: www.chinasofti.com</p>

* @author knowno

* @date 2021年3月12日

* @version 1.0

*/
package com.etc.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.etc.entity.Shopc;
import com.etc.entity.User;
import com.etc.util.AjaxResponse;
import com.etc.zmall.service.ShopCartService;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.ApiParam;

/**
 * 
 * <p>
 * Title: ShopcController
 * </p>
 * 
 * <p>
 * Description:
 * </p>
 * 
 * @author knowno
 * 
 * @date 2021年3月12日
 * 
 */
@Controller
public class ShopcController {
	Logger logger = Logger.getLogger(ShopcController.class);

	@Autowired
	private ShopCartService shopcartService;

	@PostMapping(value = "api/front/shopc.do")
	@ResponseBody
	public AjaxResponse addCart(@RequestBody Shopc shopc, HttpSession session) {

		User user = (User) session.getAttribute("user");
		logger.info("sesson: " + session.getAttribute("user"));
		shopc.setUserid(user.getUserid());
		boolean flag = shopcartService.addGoods(shopc);
		AjaxResponse res = new AjaxResponse();
		if (flag) {
			res.setCode(200);
			res.setMsg("操作成功");
		} else {
			res.setCode(0);
			res.setMsg("操作失败");
		}
		return res;

	}

	@GetMapping("api/front/shopc.do")
	public String showCart(HttpSession session,
			@ApiParam(name = "keywords", required = false, value = "商品查询关键字") @RequestParam(value = "keywords", required = false, defaultValue = "") String keywords,
			@RequestParam(value = "page", required = false, defaultValue = "1") int page,
			@RequestParam(value = "limit", required = false, defaultValue = "10") int limit, Model model) {

		User user = (User) session.getAttribute("user");
		PageInfo<Shopc> pageinfo = shopcartService.showShopCar(user.getUserid(), page, limit, keywords);
		model.addAttribute("pd", pageinfo);
		return "forward:/efront/Cart.jsp";
	}
}
